import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
import os

class Compare:
    i=1 
    def compare_main(self, str1, str2):
        # doc1 = ET.parse(str1)
        doc2 = ET.parse(self.get_new_xml(str2))
        doc3 = ET.parse(self.get_new_xml(str1))
        # ele = doc1.getroot()
        ele2 = doc2.getroot()
        ele3 = doc3.getroot()
        num = self.get_sim(ele3, ele2)
        print(num)
        sum_val = self.get_sum(ele3)
        print(sum_val)
        return num / sum_val
    
    # def get_sim(self, e, ele, num):
    #     for temp1 in e:
    #         if temp1.tag == "variable" or temp1.tag == "Other":
    #             for com in ele:
    #                 temp2 = com
    #                 if temp1.tag == "variable":
    #                     if temp1.get("Name") == temp2.get("Name") and \
    #                        temp1.get("Type") == temp2.get("Type") and \
    #                        temp1.get("Value") == temp2.get("Value") and \
    #                        temp1.get("Big") == temp2.get("Big"):
    #                         num += 1
    #                 elif temp1.tag == "Other":
    #                     if temp1.text == temp2.text:
    #                         num += 1
    #         else:
    #             num = self.get_sim(temp1, ele, num)
    #     return num
    
    def get_sum(self, e):
        return len(e)
    
    def get_sim(self, e, ele):
        num = 0
        for temp1 in e:
            for temp2 in ele:
                if temp1.tag == "variable":
                    if temp1.get("Name") == temp2.get("Name") and \
                        temp1.get("Type") == temp2.get("Type") and \
                        temp1.get("Value") == temp2.get("Value") and \
                        temp1.get("Size") == temp2.get("Size"):
                            num += 1
                            break
                elif temp1.tag == "Other":
                    if temp1.text is not None and temp2.text is not None and temp1.text.strip() == temp2.text.strip():
                        num += 1
                        break
        return num
    
    def get_new_xml(self, str_path):
        a=self.i
        new_xml_path = 'blueprints/xml/test/temp.xml'
        self.i+=1

        # Delete the existing temp.xml file if it exists
        if os.path.exists(new_xml_path):
            os.remove(new_xml_path)

        doc = ET.Element("cppcompare")
        doc_tree = ET.ElementTree(doc)

        doc1 = ET.parse(str_path)
        ele = doc1.getroot()

        if ele:
            self.get_child_xml(ele, doc)

        doc_tree.write(new_xml_path, encoding="utf-8", xml_declaration=True)
        self.format_xml(new_xml_path)  # Format the output XML
        return new_xml_path
    
    def format_xml(self,xml_path):
        dom = minidom.parse(xml_path)
        pretty_xml = dom.toprettyxml(indent="  ")
        with open(xml_path, "w", encoding="utf-8") as f:
            f.write(pretty_xml)
        
    def get_child_xml(self, e, ele):
        for temp in e:
            if temp.tag == "variable" or temp.tag == "Other":
                tempEle = ET.Element(temp.tag)
                tempEle.attrib = temp.attrib
                if temp.text:
                    tempEle.text = temp.text
                ele.append(tempEle)
            else:
                self.get_child_xml(temp, ele)

    
    # def sentence_sim(self, str1, str2):
    #     num = sum(1 for i, j in zip(str1, str2) if i == j)
    #     if str1:
    #         return num / len(str1)
    #     else:
    #         return 0

